# -*- coding: utf-8 -*-
"""
Created on Wed Jul  5 16:56:32 2017

@author: Jocker
"""
#所有Key均使用String,value类型不限
import redis
#import threading
hostaddress='47.93.103.173'
port_use=6379
#获得redis连接池
#pool = redis.ConnectionPool(host=hostaddress, port=port_use)
#           #初始化连接
#r = redis.Redis(connection_pool=pool)
class redis_client_dao:
    def __init__(self):
          #获得redis连接池
        self.pool = redis.ConnectionPool(host=hostaddress, port=port_use)
           #初始化连接
        self.r = redis.Redis(connection_pool=self.pool)
#     request:String,list,response:---------------------------------------- 
    def SetString(self,name,value,time):
        self.r.set(name,value,time)
    def GetString(self,name):
        r_get=self.r.get(name)
        if(r_get):
           return bytes.decode(r_get)
        else:
            return False
    def MutiAddToSet(self,setname,valuelist):
            for value in valuelist:
                self.r.sadd(setname,value)
#     request:String,any_but_only,response:----------------------------------------           
    def SingleAddToSet(self,setname,value):
                self.r.sadd(setname,value)
#     request:String,list,response:----------------------------------------      
    def MutiDelFromSet(self,setname,valuelist):
            for value in valuelist:
                self.r.srem(setname,valuelist)
#     request:String,any_but_only,response:----------------------------------------                      
    def SingleDelFromSet(self,setname,value):
                self.r.srem(setname,value)
#     request:String response:list----------------------------------------      
    def GetAllFromset(self,setname):
        r_set=self.r.smembers(setname)
        r_list=list(r_set)
        for i in range(0,len(r_list)):
           r_list[i]=bytes.decode(r_list[i])
        return r_list
#     request:String,dic,response:----------------------------------------              
    def MutiHset(self,hsetname,dic):    
        self.r.hmset(hsetname,dic)
#     request:String,String,any_but_only,response:----------------------------------------    
    def SingleHset(self,hsetname,key,value):
        self.r.hset(hsetname,key,value)
#     request:String,list,response:list----------------------------------------           
    def MutiHget(self,hsetname,valuelist):
        r_hset=self.r.hmget(hsetname,valuelist)  
        r_list=list(r_hset)
        for i in range(0,len(r_list)):
           r_list[i]=bytes.decode(r_list[i])
        return r_list
#     request:String,String,response:any_but_only----------------------------------------              
    def SingleHget(self,hsetname,key):
        r_get=self.r.hget(hsetname,key)
        if(r_get):
            r_get=bytes.decode(r_get)
            return r_get
        else:
            return False
#     request:String,response:dic----------------------------------------
    def HgetAll(self,hsetname):
        r_all=self.r.hgetall(hsetname)
        if(r_all):
            r_dic={}
            for key in r_all.keys(): 
                r_dic[bytes.decode(key)]=bytes.decode(r_all[key])
            return r_dic
        else:
            return False
#     request:String,response:list----------------------------------------
    def HgetAllKeys(self,hsetname):
        r_keys=self.r.hkeys(hsetname)
        for i in range(0,len(r_keys)):
           r_keys[i]=bytes.decode(r_keys[i])
        return r_keys
#     request:String,response:long----------------------------------------
    def HgetLen(self,hsetname):
        r_len=self.r.hlen(hsetname)
        return r_len
#     request:String,String,response:bool----------------------------------------
    def Hexists(self,hsetname,key):
        result=self.r.hexists(hsetname,key)
        return result
#     request:String,list,response:----------------------------------------
    def MutiHsetDel(self,hsetname,keyslist):
        for key in keyslist:
            self.r.hdel(hsetname,key)
#     request:String,String,response:----------------------------------------
    def SingleHsetDel(self,hsetname,key):
        self.r.hdel(hsetname,key)
#     ----------------------------------------
#-------------------------测试代码-------------------------------# 
redis_client=redis_client_dao()    
username=redis_client.HgetAll("109422_friendlist")
if(not username):
    print("not exists")
else:
    print(username)
#a=redis_client_dao()
#b=a.GetString("hset")
#if(b):
#    print("ok")
#else:
#    print("false")
#print("5" in b)
#def TiemOut():
##    print('Hello Timer!')
#    a.SetString("test_time","time1",5)
#    global timer
#    timer = threading.Timer(2,TiemOut)
#    timer.start()
#
##b=a.MutiHget("555_info", ["username", "password"])
##a.SetString("timer1","ok",10)
##b=a.GetString("timer1")
##print(b)
#a.SingleAddToSet("22","aa")
#a.SetString("22","aa",10)
#b=a.GetString("22")
#print(b)
#valuelist=[1,2,3,4]
#value=10
#dic={"user6":6,"user4":4,"user5":5}
#valuelist=["user1","user2"]
#a.SingleHsetDel("hset","user2")
#print(result)
#result=redis_client_dao.getAllFromset("uid")
#print(result)
